package com.example.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.R;
import com.example.models.managers.Register;

/**
 * Created with IntelliJ IDEA.
 * User: dlernatovich
 * Date: 7/12/12
 * Time: 7:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class Registration extends Activity implements View.OnClickListener {

    private Button registerButton;
    private EditText loginText;
    private EditText passwordText;
    private EditText retypePasswordText;
    private EditText emailText;
    private ProgressDialog progressDialog;

    private static final int SUCCESS_DIALOG = 0x11556654;
    private static final int REGISTRATION_FAIL = 0x22556654;

    private boolean isRegister = false;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.registration_form);

        registerButton = (Button) findViewById(R.id.registrationFormRegBtn);
        loginText = (EditText) findViewById(R.id.registrationUserNameField);
        passwordText = (EditText) findViewById(R.id.registrationFormPassword);
        retypePasswordText = (EditText) findViewById(R.id.registrationFormRetype);
        emailText = (EditText) findViewById(R.id.registrationFormEmail);
        progressDialog = new ProgressDialog(this);


        registerButton.setOnClickListener(this);

    }

    /*
    TODO Registration function
     */
    private void registration() {
        Register register = new Register();
        register.setEmail(emailText.getText().toString().trim().toLowerCase());
        register.setPassword(passwordText.getText().toString().trim());
        register.setRetypePassword(retypePasswordText.getText().toString().trim());
        register.setUsername(loginText.getText().toString().trim());
        if (register.checkPasswordAndMail()) {
            try {
                register.createNewUser();
                isRegister = true;
            } catch (Exception ex) {
                isRegister = false;
                Log.e("", "ERROR REGISTRATION", ex);
                try {
                    register.closeConnection();
                } catch (Exception e) {
                }
            }
        }
    }

    /*
    TODO Creation dialog result
     */
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder;
        switch (id) {
            case SUCCESS_DIALOG: {
                builder = new AlertDialog.Builder(this);
                builder.setMessage("Registration complete! You can authorization use you logining parameters...");
                builder.setCancelable(false);
                builder.setPositiveButton("OK", null);
                dialog = builder.create();
                break;
            }
            case REGISTRATION_FAIL: {
                builder = new AlertDialog.Builder(this);
                builder.setMessage("Registration fail! Please check the internet connection or user data...");
                builder.setCancelable(false);
                builder.setPositiveButton("OK", null);
                dialog = builder.create();
                break;
            }
            default:
                dialog = null;
        }
        return dialog;
    }

    /*
        TODO Action click listener
         */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.registrationFormRegBtn: {
                BackgroundRegister backgroundRegister = new BackgroundRegister();
                backgroundRegister.execute();
                break;
            }
            default:
                break;
        }
    }

    /*
    TODO Background task executer
     */
    private class BackgroundRegister extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(Registration.this, "Creating user", "Please wait...",
                    true, false);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
            if (isRegister) {
                showDialog(SUCCESS_DIALOG);
                registerButton.setEnabled(false);
            } else {
                showDialog(REGISTRATION_FAIL);
                registerButton.setEnabled(true);
            }
        }

        @Override
        protected Void doInBackground(Void... voids) {
            registration();
            return null;
        }
    }

}